import ddapi from './ddapi'
import Vue from 'vue'

export class AuthService {
  //签名
  autograph() {
    return new Promise(function (resolve) {
      let configUrl = "/api/auth/getPcConfig";
      Vue.http.get(configUrl).then(res => {
        let jsConfig = res.data;
        jsConfig.jsApiList = [
          'runtime.info',
          'biz.contact.choose',
          'device.notification.confirm',
          'device.notification.alert',
          'device.notification.prompt',
          'device.launcher.launchApp',
          'biz.user.get',
          "biz.util.uploadImage",
          "biz.contact.complexPicker",
          "biz.contact.choose",
          "runtime.permission.requestOperateAuthCode",
        ]; // 必填，需要使用的jsapi列表，注意：不要dd。
        ddapi.config(jsConfig);

        localStorage.setItem("corpId", jsConfig.corpId);

        console.log(jsonConfig)

        ddapi.error().then(res => {
          console.log(res)
        })

        DingTalkPC.ready(function () {
          resolve();
        });
      });
    })
  }

  //免登
  authLogin() {
    let t = this;
    // ding60943bcc4a8f79ba35c2f4657eb6378f
    return new Promise(function (resolve) {
      let corpid = localStorage.getItem('corpid');
      ddapi.requestAuthCode(corpid).then(code => {
        //alert(code);
        Vue.http.get('/api/auth/login?code=' + code).then(res => {
          //免登钥匙
          localStorage.setItem('authorization', res.data.token);
          resolve()
        }, err => {
          alert(JSON.stringify(err))
        })
      })
    });
  }
}


let authservice = new AuthService()
export default authservice;
